package com.avic.rong.search.adapter.homePageAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.search.activity.SearchHomePageActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/13 19:07
 * E-Mail Address：16601203965@163.com
 */
public class SearchHomePageBuQuanRvAdapter extends RecyclerView.Adapter<SearchHomePageBuQuanRvAdapter.SearchHomePageBuQuanRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private SearchHomePageRvListener searchHomePageRvListener;
    private List<String> dataList = new ArrayList<>();


    public void setSearchHomePageRvListener(SearchHomePageRvListener searchHomePageRvListener) {
        this.searchHomePageRvListener = searchHomePageRvListener;
    }

    public void setDataList(List<String> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public SearchHomePageBuQuanRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public SearchHomePageBuQuanRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_home_page_rv_item, viewGroup, false);
        return new SearchHomePageBuQuanRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchHomePageBuQuanRvViewHolder holder, int position) {
        SpannableString spannableString = new SpannableString(dataList.get(position));
        int startIndex = dataList.get(position).toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
        if (startIndex >= 0) {
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
            spannableString.setSpan(colorSpan, startIndex, startIndex + SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            holder.tv.setText(spannableString);
        } else {
            holder.tv.setText(dataList.get(position));
        }

        holder.itemView.setOnClickListener(new MyOnClickListener(dataList.get(position)));
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    class MyOnClickListener implements View.OnClickListener {
        String itemStr;

        public MyOnClickListener(String itemStr) {
            this.itemStr = itemStr;
        }

        @Override
        public void onClick(View v) {
            searchHomePageRvListener.setCurrentText(itemStr);
        }
    }

    class SearchHomePageBuQuanRvViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.home_page_rvitem_tv)
        TextView tv;

        public SearchHomePageBuQuanRvViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface SearchHomePageRvListener {
        void setCurrentText(String searchStr);
    }
}
